package com.xinruke.hostels.support.service;

import com.alibaba.fastjson.JSONObject;
import com.xinruke.hostels.common.cache.util.SysSqlParamUtil;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.support.interfaces.dto.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ChenXiangpeng
 * @ClassName HotelDictService
 * @date：2019/8/10
 * @version: V1.0.0
 * @description：
 */
@Service
public class HotelDictService {

    public ResultVO<List<SystemDictAllQueryResultDTO>> getAllDict(HotelDictAllQueryDTO params) {
        ResultVO<List<SystemDictAllQueryResultDTO>> resultVO = ResultVO.fail();

        //最终结果
        List<SystemDictAllQueryResultDTO> data = new ArrayList<>();

        //酒店房型
        List<SystemDictFangXingDTO> sysParams1 = SysSqlParamUtil.getSysParams("jiudian.fangxing".toUpperCase(), SystemDictFangXingDTO.class);
        SystemDictAllQueryResultDTO resultDTO1 = new SystemDictAllQueryResultDTO();
        resultDTO1.setFenzumingcheng("酒店房型");
        resultDTO1.setFenzubianma(SysDicConstant.JIUDIAN_FANGXING);
        Map<String, List<SystemDictFangXingDTO>> collect = sysParams1.stream().collect(Collectors.groupingBy(SystemDictFangXingDTO::getKey));
        List<SystemDictQueryResultDTO> list1 = new ArrayList<>();
        for (String key:collect.keySet()){
            if(params.getJiudianId().toString().equals(key)){
                List<SystemDictFangXingDTO> systemDictFangXingDTOS = collect.get(key);
                list1 = systemDictFangXingDTOS.stream().map(e -> {
                    SystemDictQueryResultDTO systemDictQueryResultDTO = new SystemDictQueryResultDTO();
                    systemDictQueryResultDTO.setCanshuzhi(e.getFangxingId());
                    systemDictQueryResultDTO.setCanshumingcheng(e.getFangxingmingcheng());
                    return systemDictQueryResultDTO;
                }).collect(Collectors.toList());
            }
        }
        resultDTO1.setSubParamList(list1);
        data.add(resultDTO1);

        //会场类型
        List<HotelDictChangxingDTO> sysParams2 = SysSqlParamUtil.getSysParams("jiudian.changxing".toUpperCase(), HotelDictChangxingDTO.class);
        SystemDictAllQueryResultDTO resultDTO2 = new SystemDictAllQueryResultDTO();
        resultDTO2.setFenzumingcheng("酒店会场类型");
        resultDTO2.setFenzubianma(SysDicConstant.JIUDIAN_CHANGXING);
        Map<String, List<HotelDictChangxingDTO>> collect2 = sysParams2.stream().collect(Collectors.groupingBy(HotelDictChangxingDTO::getKey));
        List<SystemDictQueryResultDTO> list2 = new ArrayList<>();
        for (String key:collect2.keySet()){
            if(params.getJiudianId().toString().equals(key)){
                List<HotelDictChangxingDTO> systemDictFangXingDTOS = collect2.get(key);
                list2 = systemDictFangXingDTOS.stream().map(e -> {
                    SystemDictQueryResultDTO systemDictQueryResultDTO = new SystemDictQueryResultDTO();
                    systemDictQueryResultDTO.setCanshuzhi(e.getChangxingId());
                    systemDictQueryResultDTO.setCanshumingcheng(e.getChangxingmingcheng());
                    return systemDictQueryResultDTO;
                }).collect(Collectors.toList());
            }
        }
        resultDTO2.setSubParamList(list2);
        data.add(resultDTO2);

        //会场类型
        List<HotelDictLoudongDTO> sysParams3 = SysSqlParamUtil.getSysParams("jiudian.loudong".toUpperCase(), HotelDictLoudongDTO.class);
        SystemDictAllQueryResultDTO resultDTO3 = new SystemDictAllQueryResultDTO();
        resultDTO3.setFenzumingcheng("酒店楼栋");
        resultDTO3.setFenzubianma(SysDicConstant.JIUDIAN_LOUDONG);
        Map<String, List<HotelDictLoudongDTO>> collect3 = sysParams3.stream().collect(Collectors.groupingBy(HotelDictLoudongDTO::getKey));
        List<SystemDictQueryResultDTO> list3 = new ArrayList<>();
        for (String key:collect3.keySet()){
            if(params.getJiudianId().toString().equals(key)){
                List<HotelDictLoudongDTO> systemDictFangXingDTOS = collect3.get(key);
                list3 = systemDictFangXingDTOS.stream().map(e -> {
                    SystemDictQueryResultDTO systemDictQueryResultDTO = new SystemDictQueryResultDTO();
                    systemDictQueryResultDTO.setCanshuzhi(e.getLoudongId());
                    systemDictQueryResultDTO.setCanshumingcheng(e.getMingcheng());
                    return systemDictQueryResultDTO;
                }).collect(Collectors.toList());
            }
        }
        resultDTO3.setSubParamList(list3);
        data.add(resultDTO3);

        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("success");
        resultVO.setData(data);
        return resultVO;
    }

    public static void main(String[] args) {
        Integer jiudianId = 1;
        List<SystemDictFangXingDTO> sysParams1 = SysSqlParamUtil.getSysParams("jiudian.fangxing".toUpperCase(), SystemDictFangXingDTO.class);
        SystemDictAllQueryResultDTO resultDTO2 = new SystemDictAllQueryResultDTO();
        resultDTO2.setFenzumingcheng("酒店房型");
        resultDTO2.setFenzubianma(SysDicConstant.JIUDIAN_FANGXING);
        Map<String, List<SystemDictFangXingDTO>> collect = sysParams1.stream().collect(Collectors.groupingBy(SystemDictFangXingDTO::getKey));
        List<SystemDictQueryResultDTO> list2 = new ArrayList<>();
        for (String key:collect.keySet()){
            if(jiudianId.toString().equals(key)){
                List<SystemDictFangXingDTO> systemDictFangXingDTOS = collect.get(key);
                list2 = systemDictFangXingDTOS.stream().map(e -> {
                    SystemDictQueryResultDTO systemDictQueryResultDTO = new SystemDictQueryResultDTO();
                    systemDictQueryResultDTO.setCanshuzhi(e.getFangxingId());
                    systemDictQueryResultDTO.setCanshumingcheng(e.getFangxingmingcheng());
                    return systemDictQueryResultDTO;
                }).collect(Collectors.toList());
            }
        }
        resultDTO2.setSubParamList(list2);
        System.out.println(JSONObject.toJSONString(resultDTO2));
    }
}
